package com.NeuglsWorkStudio.UI.FormElement.Component
{
	import com.NeuglsWorkStudio.UI.FormElement.Config.TConfig;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TMenuItemConfig;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TPopupMenuConfig;
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class TPopupMenu extends TComponent
	{
		private var _FBackground:Sprite;
		private var _FMenuItems:Vector.<TMenuItem>;
		private var _FMaxWidth:Number=120;
		
		public static var MenuItemHeight:Number=20;
		//public static var MenuItemWidth:Number=100;
		
		public function TPopupMenu(AConfig:TConfig)
		{
			super(AConfig);
		}
		
		public function PopupConfig():TPopupMenuConfig{
			return _FConfig as TPopupMenuConfig;
		}
		
		private function GetFunction(F:Function,ID:int,ExtraData:Object):Function{
			return function():void{
				if(F!=null){
					F(ID,ExtraData);
				}
			}
		}
		override protected function Init():void{
			super.Init();
			
			_FBackground=new Sprite();
			this.addChild(_FBackground);
			_FMenuItems=new Vector.<TMenuItem>();
			for(var I:int;I<PopupConfig().MenuItems.length;I++){
				var MenuItemConfig:TMenuItemConfig=new TMenuItemConfig();
				MenuItemConfig.Caption=PopupConfig().MenuItems[I].Caption;
				MenuItemConfig.DisplayIcon=PopupConfig().MenuItemHaveIcon;
				MenuItemConfig.Height=MenuItemHeight;
				MenuItemConfig.HoverBackAlpha=1;
				MenuItemConfig.HoverBackColor=0x0000EE;
				MenuItemConfig.NormalFontColor=0xFFFFFF;
				
				MenuItemConfig.UseIconURL=PopupConfig().MenuItems[I].UseIconURL;
				MenuItemConfig.IconURL=PopupConfig().MenuItems[I].IconURL;
				MenuItemConfig.Icon=PopupConfig().MenuItems[I].Icon;
				MenuItemConfig.IconH=16;
				MenuItemConfig.IconW=16;
				MenuItemConfig.Name='menuitempopup'+I;
				MenuItemConfig.NormalBackAlpha=PopupConfig().BackAlpha;
				MenuItemConfig.NormalBackColor=PopupConfig().BackColor;
				var ID:Number=PopupConfig().MenuItems[I].ID;
				var F:Function=PopupConfig().OnClickItem;
				var ExtraData:Object=PopupConfig().MenuItems[I].ExtraData;
				
				MenuItemConfig.OnClick=GetFunction(F,ID,ExtraData);
				MenuItemConfig.Width=PopupConfig().Width-PopupConfig().BorderSize*2;
				var MenuItem:TMenuItem=new TMenuItem(MenuItemConfig);
				_FMenuItems.push(MenuItem);
				this.addChild(MenuItem);
				_FMaxWidth=MenuItem.width+10>_FMaxWidth?MenuItem.width+10:_FMaxWidth;
			}
			
			Paint();
			
			this.addEventListener(MouseEvent.ROLL_OUT,onMouseRollOut,false,0,true);
			this.addEventListener(Event.ADDED_TO_STAGE,OnAddedToStage,false,0,true);
		}
		
		protected function Paint():void{
			var LastY:Number=5;
			var LastX:Number=1;
			var MarginY:Number=5;
			var W:Number=PopupConfig().AutoWidth?_FMaxWidth:PopupConfig().Width-PopupConfig().BorderSize*2;
			var BW:Number=W-PopupConfig().BorderSize*2;
			var H:Number=_FMenuItems.length*(MenuItemHeight+MarginY)-PopupConfig().BorderSize*2;
			var R:Number=PopupConfig().RoundSize;
			
			//draw background
			with(_FBackground.graphics){
				clear();
				lineStyle(PopupConfig().BorderSize,PopupConfig().BorderColor);
				beginFill(PopupConfig().BackColor,PopupConfig().BackAlpha);
				drawRoundRect(0,0,W,H,R,R);	
				endFill();
			}
			
			for(var I:int=0;I<_FMenuItems.length;I++){
				_FMenuItems[I].x=LastX;
				_FMenuItems[I].y=LastY;
				_FMenuItems[I].Width=BW;
				LastY+=_FMenuItems[I].height+MarginY;
			}
		}
		
		protected function onMouseRollOut(E:MouseEvent):void{
			ToDisapperar();
		}
		
		public function ToDisapperar():void{
			if(PopupConfig().OnDisappear!=null){
				PopupConfig().OnDisappear();
			}
			
			var F:Function=function(T:DisplayObject):void{
				if(T.parent){
					T.parent.removeChild(T);
				}
			}
			
			TweenLite.to(this,1,{alpha:0,onComplete:F,onCompleteParams:[this]});
		}
		
		protected function OnAddedToStage(E:Event):void{
			if(PopupConfig().OnPopup!=null){
				PopupConfig().OnPopup();
			}
			
			
			this.alpha=0;
			TweenLite.to(this,1,{alpha:1});
		}
	}
}